/*
 * riolib_cmd.c
 *
 *  Created on: 2021年3月11日
 *      Author: root
 */


#include "riolib_cmd.h"
#include "rapidio_mport_mgmt.h"
#include "riocpplib.h"

int bslRioPortInit(int index)
{
	int rc = EXIT_SUCCESS;

	rc = sem_init(&srioSemInput, 0, 0);

	if((index==0) ||(index==1))
	{
		rc=riomp_mgmt_mport_create_handle(index,O_NONBLOCK,&mport_hnd);
		return rc;
	}
	else
	{
		printf("Error! Invalid port index\n");
		return -1;
	}
}


int bslRioPortDestory()
{
	int rc = EXIT_SUCCESS;
	rc=riomp_mgmt_mport_destroy_handle(&mport_hnd);
	return rc;
}



int bslRioGetID()
{
	int rc = EXIT_SUCCESS;
	uint32_t data = 0;
	rc=riomp_mgmt_lcfg_read(mport_hnd, 0x60, 4, &data);
	if (!rc)
	{
		return data;
	}
	else
	{
		return -1;
	}
}


int bslRioSetID(uint32_t ID)
{
	int rc = EXIT_SUCCESS;
	rc=riomp_mgmt_lcfg_write(mport_hnd, 0x60, 4, ID);
	if (!rc)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}

int bslRioWriteRemoteReg(uint32_t tgt_destid ,uint8_t tgt_hc,uint32_t offset,uint32_t data)
{
	int rc = EXIT_SUCCESS;
	rc = riomp_mgmt_rcfg_write(mport_hnd, tgt_destid, tgt_hc, offset,4, data);
	if (!rc)
	{
		return 0;
	}
	else
	{
		return -1;
	}

}
int bslRioWriteLocalReg(uint32_t offset,uint32_t data)
{
	int rc = EXIT_SUCCESS;
	rc = riomp_mgmt_lcfg_write(mport_hnd, offset, 4, data);
	if (!rc)
	{
		return 0;
	}
	else
	{
		return -1;
	}

}

int bslRioReadRemoteReg(uint32_t tgt_destid ,uint8_t tgt_hc,uint32_t offset)
{
	int rc = EXIT_SUCCESS;
	uint32_t data = 0;
	rc = riomp_mgmt_rcfg_read(mport_hnd, tgt_destid, tgt_hc, offset,4, &data);
	if (!rc)
	{
		return data;
	}
	else
	{
		return -1;
	}
}
int bslRioReadLocalReg(uint32_t offset)
{
	int rc = EXIT_SUCCESS;
	uint32_t data = 0;
	rc = riomp_mgmt_lcfg_read(mport_hnd, offset, 4, &data);
	if (!rc)
	{
		return data;
	}
	else
	{
		return -1;
	}
}


